#define PC



using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

//added for debugging
#if PC
using System.IO;
using System.Configuration;
using System.Diagnostics;
using System.Threading;
#endif


namespace TimeWar
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class TimeWar : Microsoft.Xna.Framework.Game
    {
        private WidescreenGraphicsDeviceManager graphics;
        private TitleScreen timeWarTitle;
        private ChooseScenario choiceGoodBadScreen;
        private GameMap timeWarGameMap;
        private CurrGameComponent currGameComponent;

        public enum PlayerCulture { TimeLord, Dalek };

        public GameMap getGameMap()
        {
            return timeWarGameMap;
        }

        public CurrGameComponent getCurrGameComponent()
        {
            return currGameComponent;
        }

        public void setCurrGameComponent(CurrGameComponent obj)
        {
            currGameComponent = obj;
        }

        public void setGameMapToCurrGameComponent(int playerSide)
        {
            currGameComponent.Enabled = false;
            timeWarGameMap = new GameMap(this);
            timeWarGameMap.playerSide = playerSide;
            currGameComponent = timeWarGameMap;
            currGameComponent.LoadContent(this.GraphicsDevice);
        }


        public TimeWar()
        {
            graphics = new WidescreenGraphicsDeviceManager(this);
            Content.RootDirectory = ".\\Content";
            this.graphics.PreferMultiSampling = false;
            this.graphics.PreferredBackBufferWidth = 1280;
            this.graphics.PreferredBackBufferHeight = 720;

            this.graphics.IsFullScreen = false;
            this.graphics.IsWideScreenOnly = true;
            graphics.ApplyChanges();

        }


        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        /// 

        private void initializeTracing()
        {

#if PC

            String logFile = null;
            logFile = ConfigurationManager.AppSettings["traceFile"];

            if (logFile == null || logFile.Trim().Length == 0)
            {
                logFile = "timeWarTrace.txt";
            }
            FileStream traceStream = File.Create(logFile);
            TraceListener txtTracer = new TextWriterTraceListener(traceStream);
            Trace.Listeners.Add(txtTracer);

            Trace.WriteLine("[" + DateTime.Now + "]: Tracing began...");
#endif

        }

        private void endTracing()
        {
#if PC
            Trace.WriteLine("[" + DateTime.Now + "]: Program exiting...");
            Trace.Flush();
            Trace.Close();
#endif
        }

        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
            initializeTracing();
            currGameComponent = timeWarTitle;
        }

        protected override void Dispose(bool disposing)
        {
            endTracing();
            base.Dispose(disposing);
        }

        protected override void LoadContent()
        {
            timeWarTitle = new TitleScreen(this);
            timeWarTitle.LoadContent(graphics.GraphicsDevice);

            choiceGoodBadScreen = new ChooseScenario(this);
            choiceGoodBadScreen.LoadContent(graphics.GraphicsDevice);



            base.LoadContent();
        }


        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
#if PC
            Trace.WriteLine("[" + DateTime.Now + "]: Entered TimeWar.Update() method.");
#endif

            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
#if PC
                Trace.WriteLine("[" + DateTime.Now + "]: Gamepad player one back button pressed.");
#endif
                this.Exit();
            }

#if PC
            if (Mouse.GetState().RightButton == ButtonState.Pressed)
            {
                Trace.WriteLine("[" + DateTime.Now + "]: Mouse right button pressed.");
                this.Exit();

            }

            // TODO: Add your update logic here
            if (Mouse.GetState().LeftButton == ButtonState.Pressed)
            {
                Trace.WriteLine("[" + DateTime.Now + "]: Mouse left button pressed.");
                currGameComponent.Enabled = false;
                currGameComponent = choiceGoodBadScreen;

            }

#endif


            currGameComponent.Update(gameTime);


            base.Update(gameTime);

#if PC
            Trace.WriteLine("[" + DateTime.Now + "]: Exiting TimeWar.Update() method.");
#endif

        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {

            currGameComponent.Draw(graphics, gameTime);


        }


    }
}
